import $ from 'jquery'
import { createStyles } from '@material-ui/core/styles';

const debug = 'http://123215.site:12315/'

export function Get(api, params) {
  const response = $.ajax({
    url: debug + api,
    data:  $.param(params),
    cache: false,
    async: false,
    type: "GET",
  })

  console.log(response.responseJSON)
  return response.responseJSON ? response.responseJSON : {}
}

export function GetAsync(api, params, cb, cbError) {
  const response = $.ajax({
    url: debug + api,
    data:  $.param(params),
    cache: false,
    async: true,
    type: "GET",
    success:function(response){
      if (response){
        console.log(response)
        cb && cb(response || {})
      }

    },
    error: function(a, b, c) {
      cbError && cbError(a, b, c)
    }

  })
}

export function GetWithToken(api, params) {
  const response = $.ajax({
    url: debug + api + '?' + $.param({ "token": localStorage.getItem("token") }),
    data: params,
    cache: false,
    async: false,
    type: "GET",
  })
  return response.responseJSON ? response.responseJSON : {}
}

export function Post(api, params) {
  const response = $.ajax({
    url: debug + api,
    contentType: "application/json",
    data: JSON.stringify(params),
    cache: false,
    async: false,
    type: "POST",
  })
  return response.responseJSON ? response.responseJSON : {}
}

export function sleep(d){
  for(var t = Date.now();Date.now() - t <= d;);
}

export function replaceAll(m, t, str){
  let regExp = new RegExp(m,'g')

  if (!str){
    str = ""
  }
  return str.replace(regExp, t)
}

export function deepCopy( obj , deep ) {
  var o = obj instanceof Array?[]:{};
  for(var k in obj ) {
      var val = obj[k];

      if(deep && typeof val === "object" ){
          o[k] = deepCopy(val,deep);
      }else{
          o[k] = val;
      }
  }
  return o;
}

export function transList( raw, colNum=6 ){
  var res = []
  var n = 0
  const l = raw.length

  for (var i = 0; i < l ; i++){
    if (n % colNum === 0){
      res.push([])
    }

    res[res.length-1].push(raw[i])
    n++
  }

  return res
}
